package lormi.meipin.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import lormi.meipin.common.LMRongYunAPI;
import lormi.meipin.dto.ExpectWorkDto;
import lormi.meipin.entity.ExpectWork;
import lormi.meipin.entity.FocusTalent;
import lormi.meipin.entity.Position;
import lormi.meipin.entity.PositionClass;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.IExpectWorkService;

@Service
public class ExpectWorkService extends BaseService implements IExpectWorkService{

	@Override
	public void updateExpectWorkSort(Integer userId) {
		expectWorkMapper.updateExpectWorkSort(userId);
	}

	@Override
	public void addOrEditExpectWork(ExpectWorkDto expectWorkDto) throws LMException {
		PositionClass positionClass = positionClassMapper.selectByPrimaryKey(expectWorkDto.getPositionclass());
		if (positionClass==null) {
			throw new LMException(130006);
		}
		ExpectWork expectWork = expectWorkMapper.selectByMemid(expectWorkDto.getUserid());
		if (expectWork==null) {
			expectWork = new ExpectWork();
			expectWork.setMemid(expectWorkDto.getUserid());
			expectWork.setPositionclass(expectWorkDto.getPositionclass());
			expectWork.setHostcity(expectWorkDto.getHostcity());
			expectWork.setTitle(positionClass.getName());
			expectWork.setSalary(expectWorkDto.getSalary());
			expectWork.setCreatetime(new Date());
			expectWork.setUpdatetime(expectWork.getCreatetime());
			if (expectWorkMapper.insert(expectWork)<1) {
				throw new LMException(100035);
			}
		}else {
			expectWork.setPositionclass(expectWorkDto.getPositionclass());
			expectWork.setHostcity(expectWorkDto.getHostcity());
			expectWork.setTitle(positionClass.getName());
			expectWork.setSalary(expectWorkDto.getSalary());
			expectWork.setUpdatetime(new Date());
			if (expectWorkMapper.updateByPrimaryKey(expectWork)<1) {
				throw new LMException(100036);
			}
		}
		//查询匹配 并推送通知
		List<Position> positions = positionMapper.matchingPosition(expectWork.getPositionclass(), expectWork.getHostcity());
		for(Position p:positions){
			FocusTalent ft = focusTalentMapper.selectByMeridAndMemid(p.getMerid(),expectWorkDto.getUserid(),3);
			if(ft!=null)continue;
			ft = new FocusTalent();
			ft.setMemid(expectWorkDto.getUserid());
			ft.setMerid(p.getMerid());
			ft.setType(3);
			ft.setCreatetime(new Date());
			focusTalentMapper.insert(ft);
			LMRongYunAPI.push(String.valueOf(p.getMerid()),talentMapper.selectByMemId(expectWorkDto.getUserid()).getName(),32);

		}
	}

	@Override
	public ExpectWork getExpectWork(Integer userid) throws LMException {
		return expectWorkMapper.selectByMemid(userid);
	}

}
